--GameScene
--create by zouyb
--游戏场景

require "game/scene/SceneBase"
require "game/logic/base/EventConst"
require "game/ui/form/main/UIMainMap"
require "game/ui/form/main/UIBottomMenu"
require "game/ui/form/main/UITopMenu"
require "game/ui/form/dungeon/UIDungeonMgr"
require "game/ui/form/workshop/UIWorkshopGuide"

--GameScene继承自Scene
local GameScene = class("GameScene",function()
    return SceneBase.new();
end)

function GameScene.create()
    local scene = GameScene.new();
    return scene;
end

--构造函数
function GameScene:ctor()

    local uiBottom = UIBottomMenu.create();
    self:addForm(uiBottom);
    self.bottomMenu = uiBottom;

    local uiTop = UITopMenu.create();
    self:addForm(uiTop);
    self.topMenu = uiTop;

    local uiMainMap = UIMainMap.create();
    self:addForm(uiMainMap);
    self.mainMap = uiMainMap;

    local uiCommunicating = UICommunicating.create(nil, false);
    self:addForm(uiCommunicating);

    EventMgr.register("GameScene", event.ADD_FORM, function(para)
        local formName = para.name;
        local bgm = AudioM.queryFormAudioInfo(formName, "open_bgm");
        if bgm ~= nil and bgm ~= "" then
            AudioM.playBgMusic(bgm);
        end
    end);

    EventMgr.register("GameScene", event.REMOVE_FORM, function(para)
        local formName = para.name;
        local bgm = AudioM.queryFormAudioInfo(formName, "close_bgm");
        if bgm ~= nil and bgm ~= "" then
            AudioM.playBgMusic(bgm);
        end
    end);

    self:registerScriptHandler(function(ev)
        if ev == "enter" then
            -- 新手引导
            if SKIP_WORKSHOP_GUIDE ~= 1 then
                if not AlchemyWorkshopM.isGuideFinished(1) then
                    local uiForm = UIWorkshopGuide:create();
                    self:addForm(uiForm);
                elseif AlchemyWorkshopM.needShowWorkshopForm() then
                    local uiForm = UINewWorkshop:create();
                    self:addForm(uiForm);
                end
            end
        elseif ev == "exit" then
            EventMgr.removeAll("GameScene");
        end
    end);

    self:playBgMusic();
end

-- 是否处于主地图界面
function GameScene:isInMainMap()
    local num = #self.forms;
    -- 当只有MainMap、TopMenu和BottomMenu三个窗口的时候，认为此时处于主界面
    return num == 3;
end

-- 当前窗口数量
function GameScene:getFormsNum()
    return #self.forms;
end

--播放背景音乐
function GameScene:playBgMusic()
    if not AlchemyWorkshopM.isGuideFinished(1) then
        -- 先停止背景音乐
        AudioM.stopPlayBgMusic(true);
    elseif not AlchemyWorkshopM.isAllGuideFinished() then
        local bgm = AudioM.queryFormAudioInfo("bgm_workshop", "open_bgm");
        AudioM.playBgMusic(bgm);
    else
        local bgm = AudioM.queryFormAudioInfo("UIMainMap", "open_bgm");
        AudioM.playBgMusic(bgm);
    end
end

return GameScene
